package com.treasure.hunt.mode

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.module.lemlin.base.BaseViewModel
import com.module.lemlin.mode.ViewModelSubscribe
import com.module.lemlin.rxhttp.HttpServiceData
import com.module.lemlin.rxhttp.HttpServiceResponse
import com.treasure.hunt.api.*
import com.treasure.hunt.entity.*
import io.reactivex.rxjava3.disposables.Disposable

class GoodsViewMode(application: Application) : AdViewMode(application) {

    val goodsLiveData = MutableLiveData<HttpServiceResponse<GoodsResponse>>()
    val categoryLiveData = MutableLiveData<HttpServiceResponse<CategoryResponse>>()
    val searchLiveData = MutableLiveData<HttpServiceResponse<SearchResponse>>()
    val noticeLiveData = MutableLiveData<HttpServiceResponse<NoticeResponse>>()
    val baseLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()
    val mergeLiveData = MutableLiveData<HttpServiceResponse<MergeResponse>>()
    val synthesisLiveData = MutableLiveData<HttpServiceResponse<SynthesisResponse>>()
    val goodsCollectionLiveData = MutableLiveData<HttpServiceResponse<GoodsCollectionResponse>>()

    val mGoodsRequest: GoodsRequest = GoodsRequest()

    fun indexGetGoods(isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<GoodsResponse>(goodsLiveData) {
            override fun onSuccess(resp: GoodsResponse) {
                resp.loadMore = mGoodsRequest.page > 1
                super.onSuccess(resp)
                mGoodsRequest.page += 1
            }
        }
        if (!isLoadMore)
            mGoodsRequest.page = 1
        val disposable: Disposable = indexGetGoods(mGoodsRequest, listener)
        dispose.add(disposable)
    }

    fun indexGetCategory() {
        val disposable: Disposable = indexGetCategory(ViewModelSubscribe(categoryLiveData))
        dispose.add(disposable)
    }

    fun indexAddGoodsCollection(goodsId: Int) {
        val listener = object : ViewModelSubscribe<GoodsCollectionResponse>(goodsCollectionLiveData){
            override fun onSuccess(resp: GoodsCollectionResponse) {
                super.onSuccess(resp.apply { this.goodsId = goodsId })
            }
        }
        val disposable: Disposable = indexAddGoodsCollection(goodsId, listener)
        dispose.add(disposable)
    }

    fun indexSynthesisGoods(goodsId: Int) {
        val disposable: Disposable = indexSynthesisGoods(goodsId, ViewModelSubscribe(synthesisLiveData))
        dispose.add(disposable)
    }

    fun indexGetSearchWords() {
        val disposable: Disposable = indexGetSearchWords(ViewModelSubscribe(searchLiveData))
        dispose.add(disposable)
    }

    fun indexGetNotice(page: Int = 1, limit: Int = 20) {
        val disposable: Disposable = indexGetNotice(page, limit, ViewModelSubscribe(noticeLiveData))
        dispose.add(disposable)
    }

    fun indexGetOrderLog(page: Int = 1, limit: Int = 500) {
        val disposable: Disposable =
            indexGetOrderLog(page, limit, ViewModelSubscribe(mergeLiveData))
        dispose.add(disposable)
    }
}